package com.example.software.controller;

import java.util.*;

// import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.example.software.dto.ApplyEntityDTO;
import com.example.software.entity.Result;
import com.example.software.entity.WorkdayEntity;
import com.example.software.service.WorkdayService;
import com.example.software.vo.ApplyEntityVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.example.software.entity.ApplyEntity;
import com.example.software.service.ApplyService;
import com.example.software.utils.PageUtils;
import com.example.software.utils.R;



/**
 * 申请表
 *
 * @author wxy
 * @email wxy9496099@163.com
 * @date 2024-10-28 10:54:37
 */
@RestController
@Slf4j
@Api(tags = "申请相关接口")
@RequestMapping("software/apply")
public class ApplyController {
    @Autowired
    private ApplyService applyService;


    @Autowired
    private WorkdayService workdayService;

    //查一个多表查询(给管理员审批用的)



    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询出全部申请详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", required = true),
            @ApiImplicitParam(name = "size", value = "每页数量",dataType = "int"),
            @ApiImplicitParam(name = "doctorId", value = "医生id，如果查全部可以不传或者传0"),
            @ApiImplicitParam(name = "state", value = "状态，0:拒绝1:同意2:等待处理,查全部可以不传或者传3"),
    })
    public Result<PageUtils> list(@RequestParam String page,
                                  @RequestParam String size,
                                  @RequestParam Long doctorId
                    , @RequestParam Integer state){

        Map<String, Object> params = new HashMap<>();
        params.put("page",page);
        params.put("limit",size);

        if (doctorId!=null && doctorId!= 0 ){
            params.put("doctorId",doctorId);
        }

        if (state!=null && state!= 3){
            params.put("state",state);
        }
        PageUtils page2 = applyService.queryPage(params);
        List<ApplyEntityVO> list = new ArrayList<>();


        for (Object o : page2.getList()){
            ApplyEntity applyEntity = (ApplyEntity) o;
            ApplyEntityVO applyEntityVO = new ApplyEntityVO();
            BeanUtils.copyProperties(applyEntity,applyEntityVO);
            WorkdayEntity workdayEntity = workdayService.getById(applyEntity.getWorkdayId());
            applyEntityVO.setWorkTime(workdayEntity.getWorkTime());
            applyEntityVO.setAmpm(workdayEntity.getAmpm());
            list.add(applyEntityVO);
        }

        PageUtils page3 = new PageUtils(list, page2.getTotalCount(), page2.getPageSize(), page2.getCurrPage());

        return Result.success(page3);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "根据id获取详情")
    // @RequiresPermissions("software:apply:info")
    public R info(@PathVariable("id") Integer id){

		ApplyEntity apply = applyService.getById(id);

        return R.ok().put("apply", apply);
    }

    /**
     * 保存
     */
    @PostMapping
    @ApiOperation(value = "新增申请")
    // @RequiresPermissions("software:apply:save")
    public R save(@RequestBody ApplyEntityDTO applyEntityDTO){

        ApplyEntity apply = new ApplyEntity();

        //通过工作日星期几、医生id、上午下午来确定workdayId
        Long workdayId = workdayService.getWorkdayID(applyEntityDTO);

        BeanUtils.copyProperties(applyEntityDTO,apply);
        apply.setWorkdayId(workdayId);

		applyService.save(apply);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation(value = "修改申请")
    // @RequiresPermissions("software:apply:update")
    public R update(@RequestBody ApplyEntityDTO applyEntityDTO){


        ApplyEntity apply = new ApplyEntity();

        //通过工作日星期几、医生id、上午下午来确定workdayId
        Long workdayId = workdayService.getWorkdayID(applyEntityDTO);

        BeanUtils.copyProperties(applyEntityDTO,apply);
        apply.setWorkdayId(workdayId);


        applyService.updateById(apply);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping
    @ApiOperation(value = "根据id数组批量删除")
    // @RequiresPermissions("software:apply:delete")
    public R delete(@RequestBody Integer[] ids){
		applyService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
